<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="10">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>程序号维护</span>
          </div>
          <div class="head-container">
            <div v-if="crud.props.searchToggle">
              <!-- 搜索 -->
              <el-select v-model="query.operationCode" clearable filterable
                         placeholder="工序编号" style="width: 165px;" class="filter-item" @change="crud.toQuery" >
                <el-option
                  v-for="item in operations"
                  :key="item.id"
                  :label="item.operationName"
                  :value="item.operationCode" >
                  <span style="float: left">{{ item.operationName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.operationCode }}</span>
                </el-option>
              </el-select>

              <el-input v-model="query.programNo" clearable placeholder="程序号" style="width: 100px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
              <rrOperation :crud="crud" />
            </div>
            <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
            <crudOperation :permission="permission" />
            <!--表单组件-->
            <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
              <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
                <el-form-item label="工序编号" prop="operationCode">
                  <el-select v-model="form.operationCode" style="width: 370px" filterable placeholder="请选择">
                    <el-option
                      v-for="item in operations"
                      :key="item.id"
                      :label="item.operationName"
                      :value="item.operationCode" >
                      <span style="float: left">{{ item.operationName }}</span>
                      <span style="float: right; color: #8492a6; font-size: 13px">{{ item.operationCode }}</span>
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="程序号" prop="programNo">
                  <el-input v-model="form.programNo" style="width: 370px;" />
                </el-form-item>
                <el-form-item label="描述">
                  <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="text" @click="crud.cancelCU">取消</el-button>
                <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
              </div>
            </el-dialog>
            <!--表格渲染-->
            <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
              <el-table-column type="selection" width="55" />
              <el-table-column prop="operationCode" label="工序编号" width="100px;" align="center"/>
              <el-table-column prop="operationCode" label="工序名称" width="150px;" align="center">
                <template slot-scope="scope">
                  {{ getOperationName(scope.row.operationCode) }}
                </template>
              </el-table-column>
              <el-table-column prop="programNo" label="程序号" width="80px;" align="center"/>
              <el-table-column prop="remark" label="描述" width="250px;" align="center"/>
              <el-table-column v-if="checkPer(['admin','craftProgram:edit','craftProgram:del'])" fixed="right" label="操作" width="150px" align="center">
                <template slot-scope="scope">
                  <udOperation
                    :data="scope.row"
                    :permission="permission"
                  />
                </template>
              </el-table-column>
            </el-table>
            <!--分页组件-->
            <pagination />
          </div>
        </el-card>
      </el-col>
      <el-col :span="14">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>产品关联程序号</span>
          </div>
          <ProductProgram></ProductProgram>
        </el-card>
      </el-col>
    </el-row>
    <!--工具栏-->

  </div>
</template>

<script>
import crudCraftProgram from '@/api/craft/craftProgram'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import ProductProgram from '../product_program/index'
import crudCraftOperation from '@/api/craft/craftOperation'

const defaultForm = { id: null, operationCode: null, programNo: null, remark: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'CraftProgram',
  components: { pagination, crudOperation, rrOperation, udOperation ,ProductProgram},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '产品程序号',
      url: 'api/craftProgram',
      idField: 'id',
      sort: 'id,desc',
      optShow: {
        add: true,
        edit: true,
        del: true,
        download: false,
        reset: true,
        import: false
      },
      crudMethod: { ...crudCraftProgram }})
  },
  data() {
    return {
      operations : [],
      permission: {
        add: ['admin', 'craftProgram:add'],
        edit: ['admin', 'craftProgram:edit'],
        del: ['admin', 'craftProgram:del']
      },
      rules: {
        operationCode: [
          {required: true, message: '工序编码不能为空', trigger: 'blur'}
        ],
        programNo: [
          {required: true, message: '程序号不能为空', trigger: 'blur'}
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'id' },
        { key: 'operationCode', display_name: '工序编号' },
        { key: 'programNo', display_name: '程序号' },
        { key: 'remark', display_name: '描述' },
        { key: 'createBy', display_name: '创建人' },
        { key: 'createTime', display_name: '创建时间' },
        { key: 'updateBy', display_name: '更新人' },
        { key: 'updateTime', display_name: '更新时间' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getOperationName(code){
      let name = '';
      this.operations.forEach(res =>{
        if(res.operationCode == code){
          name = res.operationName;
        }
      });
      return name;
    }
  },
  mounted() {
    //加载工序
    crudCraftOperation.list().then(res => {
      this.operations = res;
    })
  }
}
</script>

<style scoped>

</style>
